package com.sg.service.base.framework.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.framework.req.*;
import com.sg.entity.ChangeColumn;
import com.sg.service.base.framework.MChangeColumnService;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.dal.mapper.mbg.ChangeColumnMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MChangeColumnServiceImpl
        extends ServiceImpl<ChangeColumnMapper, ChangeColumn>
        implements MChangeColumnService {

    @Autowired
    ChangeColumnMapper changeColumnMapper;

    /**
     * 1-1-02批量改底层变更字段[1630]
     * gen by moon at 10/16/2022, 11:06:01 AM
     */
    @Trace(operationName = "1-1-02批量改底层变更字段")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateBaseChangeCol(List<ChangeColumn> changeColumn) {

        if (changeColumn == null || changeColumn.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(changeColumn);

    }

    /**
     * 1-1-02批量查询变更字段列表ByCodes[1519]
     * gen by moon at 10/16/2022, 11:06:00 AM
     */
    @Trace(operationName = "1-1-02批量查询变更字段列表ByCodes")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<ChangeColumn> batchQueryBaseChangeColListByCodes(BatchQueryBaseChangeColListByCodesReq reqDto) {

        try {
            QueryWrapper<ChangeColumn> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getBaseChangeColumnList() == null || reqDto.getBaseChangeColumnList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("object_property_code", reqDto.getBaseChangeColumnList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(ChangeColumn.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(ChangeColumn.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(ChangeColumn.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<ChangeColumn>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-1-02批量新增底层变更字段[274]
     * gen by moon at 10/16/2022, 11:05:58 AM
     */
    @Trace(operationName = "1-1-02批量新增底层变更字段")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchAddBaseChangeCol(List<ChangeColumn> changeColumn) {

        if (changeColumn == null || changeColumn.size() == 0) {
            return false;
        }

        return super.saveBatch(changeColumn.stream().map(item -> {
            item.setChangeColumnId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
            item.setIsArchive("FALSE");
            item.setArchivngType("NOT_ARCHIVE");
            item.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            item.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            if (item != null && item.getSubjectLifeCycle() != null && item.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
                item.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
            }
            item.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            item.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            if (item != null && item.getSubjectLifeCycle() != null && item.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
                item.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
            }
            item.setIsValid("FALSE");
            item.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            item.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
            return item;
        }).collect(Collectors.toList()));
    }

    /**
     * 1-1-02查询底层变更字段列表[8]
     * gen by moon at 10/16/2022, 11:05:57 AM
     */
    @Trace(operationName = "1-1-02查询底层变更字段列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<ChangeColumn> queryBaseChangeColList(QueryBaseChangeColListReq reqDto) {

        try {
            QueryWrapper<ChangeColumn> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(ChangeColumn.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(ChangeColumn.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(ChangeColumn.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<ChangeColumn>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-1-02批量删除底层框架服务变更字段[7]
     * gen by moon at 10/16/2022, 11:05:57 AM
     */
    @Trace(operationName = "1-1-02批量删除底层框架服务变更字段")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteBaseChangeColumn(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 1-1-02删除底层框架服务变更字段[917]
     * gen by moon at 10/16/2022, 11:05:59 AM
     */
    @Trace(operationName = "1-1-02删除底层框架服务变更字段")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBaseChangeColumn(String changeColumnId) {

        return super.removeById(changeColumnId);
    }

    /**
     * 1-1-02批量查询变更字段列表ByIds[310]
     * gen by moon at 10/16/2022, 11:05:59 AM
     */
    @Trace(operationName = "1-1-02批量查询变更字段列表ByIds")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<ChangeColumn> batchQueryBaseChangeColListByIds(BatchQueryBaseChangeColListByIdsReq reqDto) {

        try {
            QueryWrapper<ChangeColumn> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getChangeEntityId() == null || reqDto.getChangeEntityId().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("entity_id", reqDto.getChangeEntityId());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(ChangeColumn.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(ChangeColumn.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(ChangeColumn.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<ChangeColumn>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-1-02修改底层变更字段[6]
     * gen by moon at 10/16/2022, 11:05:56 AM
     */
    @Trace(operationName = "1-1-02修改底层变更字段")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateBaseChangeCol(ChangeColumn changeColumn) {
        changeColumn.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        changeColumn.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        return super.updateById(changeColumn);
    }

    /**
     * 1-1-02新增底层框架服务变更字段[5]
     * gen by moon at 10/16/2022, 11:05:55 AM
     */
    @Trace(operationName = "1-1-02新增底层框架服务变更字段")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addBaseChangeColumn(ChangeColumn changeColumn) {
        changeColumn.setChangeColumnId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        changeColumn.setIsArchive("FALSE");
        changeColumn.setArchivngType("NOT_ARCHIVE");
        changeColumn.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        changeColumn.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (changeColumn != null && changeColumn.getSubjectLifeCycle() != null && changeColumn.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            changeColumn.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        changeColumn.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        changeColumn.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (changeColumn != null && changeColumn.getSubjectLifeCycle() != null && changeColumn.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            changeColumn.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        changeColumn.setIsValid("FALSE");
        changeColumn.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        changeColumn.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(changeColumn)) {
            return changeColumn.getChangeColumnId();
        } else {
            return "-1";
        }

    }

    /**
     * 1-1-02查询底层变更字段详情[3536]
     * gen by moon at 10/16/2022, 11:06:01 AM
     */
    @Trace(operationName = "1-1-02查询底层变更字段详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ChangeColumn queryBaseChangeColDetail(QueryBaseChangeColDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            QueryWrapper<ChangeColumn> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<ChangeColumn> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                ChangeColumn retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * M1-1-02执行变更字段【循环开始】[4332]
     * gen by moon at 10/16/2022, 11:06:02 AM
     */
    @Trace(operationName = "M1-1-02执行变更字段【循环开始】")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ChangeColumn implementChangeColLoopStart(ImplementChangeColLoopStartReq reqDto) {

        try {

            QueryWrapper<ChangeColumn> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<ChangeColumn> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                ChangeColumn retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
